<?php

namespace app\admin\framework\controller;

use app\admin\framework\api\Permission;
use app\common\model\Option;
use Shopwwi\LaravelCache\Cache;

#[Permission('系统设置', 'System')]
class SettingController
{
    private $noNeedAuth = ['detail'];
    
    public function detail()
    {
        $list = Option::column('option_value', 'option_name');
        return apiSuccess(data: array_map(fn($item) => $item ? json_decode($item, true) : [], $list));
    }
    
    #[Permission('更新')]
    public function update()
    {
        if (request()->method() === 'POST') {
            $name = request()->post('name', '');
            $data = request()->post('data', []);
            if (!setOption($name, $data, true)) {
                return apiError('修改失败');
            }
            return apiSuccess('保存成功');
        }
    }
    
    #[Permission('清除缓存')]
    public function clearCache()
    {
        Cache::forget('em_admin_menu');
        return apiSuccess('清除缓存成功，请刷新页面');
    }
    
}